//
//  SYShangBaoViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/2/25.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYShangBaoViewController.h"
#import "SYTopLabView.h"
#import "SYShangBaoTableViewCell.h"
#import "SYJiaoCaiViewController.h"
#import "SYJiaoCaiView.h"
#import "SYZuChengYuanView.h"

@interface SYShangBaoViewController () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (weak, nonatomic) IBOutlet UILabel *noLabel;
@property (strong, nonatomic) SYJiaoCaiView *jiaoCaiVC;
@property (strong, nonatomic) SYZuChengYuanView *zcyView;

@end

@implementation SYShangBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _curTableView.delegate = self;
    _curTableView.dataSource = self;
    _dataListArr = [NSMutableArray array];
    [_curTableView registerCellWithNIbNames:@[@"SYShangBaoTableViewCell"]];
    _curTableView.tableFooterView = [UIView new];
}
- (void)viewDidSlidInto {
    [self requestData];
}

- (void)requestData {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestGetGroupInfoName:@"" pageIndex:1 pageSize:10000 success:^(NSArray *arr) {
        [MBProgressHUD mj_hideHUD];
        [weakSelf.dataListArr addObjectsFromArray:arr];
        if (weakSelf.dataListArr.count == 0) {
            self.noLabel.hidden = NO;
        } else {
            self.noLabel.hidden = YES;
        }
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        self.noLabel.hidden = YES;
    }];
}


#pragma mark - table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
//    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYShangBaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYShangBaoTableViewCell" forIndexPath:indexPath];
    NSDictionary *dict = self.dataListArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.zuMingLab.text = [dict nilObjectForKey:@"resourceGroupName"];
    [cell.shuLiangBtn setTitle:[dict nilObjectForKey:@"packageCount"] forState:UIControlStateNormal];
    [cell.renShuBtn setTitle:[dict nilObjectForKey:@"memberCount"] forState:UIControlStateNormal];
    cell.chuangRenLab.text = [dict nilObjectForKey:@"createUser"];
//    cell.chuangShiLab.text = [dict nilObjectForKey:@"createDate"];
    NSString *timeStr = [dict nilObjectForKey:@"createDate"];
    timeStr = [timeStr substringFromIndex:timeStr.length - 3];//先这样写。后期有时间优化。标记 TOD
    cell.chuangShiLab.text = [[dict nilObjectForKey:@"createDate"] stringByReplacingOccurrencesOfString:timeStr withString:@""];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        if (index == 1) {
            [weakSelf pushJiaoCaiBao:[dict nilObjectForKey:@"resourceGroupId"]];
        } else {
            [weakSelf pushZuChengYuan:[dict nilObjectForKey:@"resourceGroupId"]];
        }
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    [headerView addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(headerView);
    }];
    return headerView;
}
- (void)pushJiaoCaiBao:(NSString *)groupid {
    _jiaoCaiVC = [[SYJiaoCaiView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
    [self.view addSubview:_jiaoCaiVC];
    __weak typeof(self) weakSelf = self;
    [NSObject executeRunloop:^{
        //        先注销了，等有数据了几开注释
                [self.jiaoCaiVC requestDataGroupId:groupid];
    } afterDelay:kAnimate];
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.jiaoCaiVC.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
    }];
    
}

- (void)pushZuChengYuan:(NSString *)groupId {
    _zcyView = [[SYZuChengYuanView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
    [self.view addSubview:_zcyView];
    __weak typeof(self) weakSelf = self;
    [NSObject executeRunloop:^{
        //        先注销了，等有数据了几开注释
            [self.zcyView requestDataGroupId:groupId];
    } afterDelay:kAnimate];
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.zcyView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
    }];
}


#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
   
        xiaoArr = @[@"组名称",@"教材数量",@"成员数量",@"创建人",@"创建时间"];
        bili = 0.2;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
